package com.yc.easy.res;

import com.yc.easy.bean.EasyProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @program: C0-S3-Ply-hwc-single
 * @description:
 * @author: 作者
 * @create: 2022-11-17 11:36
 */
@Component
public class DBRes {
    private Map<String, DataSource> ds = new HashMap<>();

    @Resource
    ApplicationContext cxt;

    public DBRes(EasyProperties easyProperties) {
        defaultJdbcTemplateName = easyProperties.getDefaultDb();
        System.out.println("easyProperties = " + easyProperties);
        easyProperties.getDb().forEach((dbname,map)->{
            String driver = map.get("driver");
            String url = map.get("url");
            String username = map.get("username");
            String password = map.get("password");
            DriverManagerDataSource dmds = new DriverManagerDataSource(url, username, password);
            dmds.setDriverClassName(driver);
            System.out.printf("数据源: %s\n" +
                    "     driver: %s\n" +
                    "        url: %s\n" +
                    "   username: %s\n" +
                    "   password: %s\n", dbname, driver, url, username, password);
            ds.put(dbname, dmds);
        });
    }

    public Map<String, DataSource> getDs() {
        return ds;
    }

    @PostConstruct
    public void init() {
        getDs().forEach((k, db) -> {
            String name = "jdbc_" + k;
            buildBean(JdbcTemplate.class, name, "dataSource", db);
            System.out.printf("构建 JdbcTemplate : %s\n", name);
            if (defaultJdbcTemplateName == null) {
                defaultJdbcTemplateName = k;
            }
        });
    }

    public void buildBean(Class<?> aClass, String id, Object... values) {
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(aClass);
        for (int i = 0; i < values.length; i += 2) {
            builder.addPropertyValue((String) values[i], values[i + 1]);
        }
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) cxt;
        registry.registerBeanDefinition(id, builder.getBeanDefinition());
    }

    private String defaultJdbcTemplateName;

    public JdbcTemplate getJdbc(String name) {
        if (name == null) {
            name = defaultJdbcTemplateName;
        }
        return (JdbcTemplate) cxt.getBean("jdbc_" + name);
    }

}
